package palmtop.com.palmtop.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.beardedhen.androidbootstrap.AwesomeTextView;
import com.google.gson.Gson;

import net.grandcentrix.tray.AppPreferences;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import palmtop.com.palmtop.R;
import palmtop.com.palmtop.model.Processtask;
import palmtop.com.palmtop.model.Taskgrid;
import palmtop.com.palmtop.util.Config;
import palmtop.com.palmtop.util.FuncUtil;
import palmtop.com.palmtop.util.NetUtil;
import palmtop.com.palmtop.util.PlaybackDialogFragment;
import palmtop.com.palmtop.util.RecordingItem;
import palmtop.com.palmtop.util.ToastUtil;

public class ProcessdetailActivity extends BaseActivity {

    @BindView(R.id.mtitle)
    TextView mtitle;
    @BindView(R.id.pname)
    AwesomeTextView pname;
    @BindView(R.id.addtime)
    AwesomeTextView addtime;
    @BindView(R.id.taskaddress)
    AwesomeTextView taskaddress;
    @BindView(R.id.pnote)
    AwesomeTextView pnote;


    Taskgrid taskgrid;
    Processtask processtask;
    @BindView(R.id.confirm)
    Button confirm;
    @BindView(R.id.reject)
    Button reject;
    @BindView(R.id.zphoto1)
    ImageView zphoto1;
    @BindView(R.id.zphoto2)
    ImageView zphoto2;
    @BindView(R.id.zphoto3)
    ImageView zphoto3;
    @BindView(R.id.zphoto4)
    ImageView zphoto4;
    @BindView(R.id.mainlay)
    LinearLayout mainlay;
    @BindView(R.id.slay)
    ScrollView slay;
    @BindView(R.id.blay)
    LinearLayout blay;
    String role_id;
    @BindView(R.id.terminal)
    Button terminal;
    @BindView(R.id.play)
    Button play;
    @BindView(R.id.lookmapBtn)
    Button lookmapBtn;

    MyApplication app;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_processdetail, Config.toptitle, 1);
        ButterKnife.bind(this);
        app = (MyApplication) getApplication();
        app.addActivityList(ProcessdetailActivity.this);
        taskgrid = (Taskgrid) getIntent().getSerializableExtra("taskgrid");
        processtask = (Processtask) getIntent().getSerializableExtra("processtask");
        initView();
    }

    private void initView() {
        mtitle.setText("信息详情");

        AppPreferences appPreferences = new AppPreferences(ProcessdetailActivity.this);
        role_id = appPreferences.getString("role_id", "");
        /*if (role_id.equals("73006b03b7f447bf953d065462e5b1ac")) {
            blay
        }*/
        /*if (role_id.equals("d18ac0ef29ee477f8e3dd214245a1de5")) {
            confirm.setVisibility(View.GONE);
            reject.setVisibility(View.GONE);
        }*/
        if (taskgrid != null) {
            if (taskgrid.getId() != 0) {
                RequestParams params = new RequestParams(Config.SERVER_URL + "processtasks/processtasks/getListByTaskidAndPuserid");
                params.addBodyParameter("taskid", String.valueOf(taskgrid.getId()));
                if (taskgrid.getStatus().equals("4")) {
                    params.addBodyParameter("regionid", String.valueOf(taskgrid.getUnitid()));
                } else {
                    params.addBodyParameter("puserid", String.valueOf(taskgrid.getReceiverid()));
                }

                NetUtil.postData(params, new Callback.CommonCallback<String>() {
                    @Override
                    public void onSuccess(String result) {
                        if (!(result == null || result.equals("\"\""))) {
                            Gson gson = new Gson();
//                        Type listType = new TypeToken<ArrayList<Processtask>>() {
//                        }.getType();
                            processtask = gson.fromJson(result, Processtask.class);
                            String taskid = String.valueOf(processtask.getTaskid());
                            String ssgrid = String.valueOf(processtask.getRegionid());
                            String puserid = String.valueOf(processtask.getPuserid());
                            FuncUtil.updateReadTask(taskid,ssgrid,puserid);
                            if (TextUtils.isEmpty(processtask.getRecordurl())) {
                                play.setEnabled(false);
                            }
                            if (processtask.getStatus().equals("0") && role_id.equals("d18ac0ef29ee477f8e3dd214245a1de5")) {
                                blay.setVisibility(View.VISIBLE);
                            }
                            pname.setText(processtask.getPname());
                            addtime.setText(processtask.getAddtime());
                            taskaddress.setText(processtask.getTaskaddress());
                            pnote.setText(processtask.getPnote());

                            if (processtask.getPhoto1() != null && !processtask.getPhoto1().equals("")) {
                                x.image().bind(zphoto1, Config.IMG_URL + processtask.getPhoto1());
                                FuncUtil.clickShowBigPic(Config.IMG_URL + processtask.getPhoto1(), zphoto1, ProcessdetailActivity.this);
                            }
                            if (processtask.getPhoto2() != null && !processtask.getPhoto2().equals("")) {
                                x.image().bind(zphoto2, Config.IMG_URL + processtask.getPhoto2());
                                FuncUtil.clickShowBigPic(Config.IMG_URL + processtask.getPhoto2(), zphoto2, ProcessdetailActivity.this);
                            }
                            if (processtask.getPhoto3() != null && !processtask.getPhoto3().equals("")) {
                                x.image().bind(zphoto3, Config.IMG_URL + processtask.getPhoto3());
                                FuncUtil.clickShowBigPic(Config.IMG_URL + processtask.getPhoto3(), zphoto3, ProcessdetailActivity.this);
                            }
                            if (processtask.getPhoto4() != null && !processtask.getPhoto4().equals("")) {
                                x.image().bind(zphoto4, Config.IMG_URL + processtask.getPhoto4());
                                FuncUtil.clickShowBigPic(Config.IMG_URL + processtask.getPhoto4(), zphoto4, ProcessdetailActivity.this);
                            }
                        } else {
                            mainlay.setVisibility(View.GONE);

                        }

                    }

                    @Override
                    public void onError(Throwable ex, boolean isOnCallback) {
                    }

                    @Override
                    public void onCancelled(CancelledException cex) {
                    }

                    @Override
                    public void onFinished() {
                    }
                });
            } else {
//            mainlay.setVisibility(View.GONE);
                slay.setVisibility(View.GONE);
                blay.setVisibility(View.GONE);
            }
        }

        if (role_id.equals("d18ac0ef29ee477f8e3dd214245a1de5") && taskgrid == null) {
            if (processtask != null) {

                if (processtask.getStatus().equals("0") && role_id.equals("d18ac0ef29ee477f8e3dd214245a1de5")) {
                    blay.setVisibility(View.VISIBLE);
                }
                pname.setText(processtask.getPname());
                addtime.setText(processtask.getAddtime());
                taskaddress.setText(processtask.getTaskaddress());
                pnote.setText(processtask.getPnote());

                if (processtask.getPhoto1() != null && !processtask.getPhoto1().equals("")) {
                    x.image().bind(zphoto1, Config.IMG_URL + processtask.getPhoto1());
                    FuncUtil.clickShowBigPic(Config.IMG_URL + processtask.getPhoto1(), zphoto1, ProcessdetailActivity.this);
                }
                if (processtask.getPhoto2() != null && !processtask.getPhoto2().equals("")) {
                    x.image().bind(zphoto2, Config.IMG_URL + processtask.getPhoto2());
                    FuncUtil.clickShowBigPic(Config.IMG_URL + processtask.getPhoto2(), zphoto2, ProcessdetailActivity.this);
                }
                if (processtask.getPhoto3() != null && !processtask.getPhoto3().equals("")) {
                    x.image().bind(zphoto3, Config.IMG_URL + processtask.getPhoto3());
                    FuncUtil.clickShowBigPic(Config.IMG_URL + processtask.getPhoto3(), zphoto3, ProcessdetailActivity.this);
                }
                if (processtask.getPhoto4() != null && !processtask.getPhoto4().equals("")) {
                    x.image().bind(zphoto4, Config.IMG_URL + processtask.getPhoto4());
                    FuncUtil.clickShowBigPic(Config.IMG_URL + processtask.getPhoto4(), zphoto4, ProcessdetailActivity.this);
                }
            } else {
//            mainlay.setVisibility(View.GONE);
                slay.setVisibility(View.GONE);
                blay.setVisibility(View.GONE);
            }
        }


        /*if (processtask != null) {
            pname.setText(processtask.getPname());
            addtime.setText(processtask.getAddtime());
            taskaddress.setText(processtask.getTaskaddress());
            pnote.setText(processtask.getPnote());
            AppPreferences appPreferences = new AppPreferences(ProcessdetailActivity.this);
            String role_id = appPreferences.getString("role_id","");
            if(role_id.equals("73006b03b7f447bf953d065462e5b1ac")){
                confirm.setVisibility(View.GONE);
                reject.setVisibility(View.GONE);
            }


            if (processtask.getPhoto1() != null && !processtask.getPhoto1().equals("")) {
                x.image().bind(zphoto1, Config.IMG_URL + processtask.getPhoto1());
                FuncUtil.clickShowBigPic(Config.IMG_URL + processtask.getPhoto1(), zphoto1, ProcessdetailActivity.this);
            }
            if (processtask.getPhoto2() != null && !processtask.getPhoto2().equals("")) {
                x.image().bind(zphoto2, Config.IMG_URL + processtask.getPhoto2());
                FuncUtil.clickShowBigPic(Config.IMG_URL + processtask.getPhoto2(), zphoto2, ProcessdetailActivity.this);
            }
            if (processtask.getPhoto3() != null && !processtask.getPhoto3().equals("")) {
                x.image().bind(zphoto3, Config.IMG_URL + processtask.getPhoto3());
                FuncUtil.clickShowBigPic(Config.IMG_URL + processtask.getPhoto3(), zphoto3, ProcessdetailActivity.this);
            }
            if (processtask.getPhoto4() != null && !processtask.getPhoto4().equals("")) {
                x.image().bind(zphoto4, Config.IMG_URL + processtask.getPhoto4());
                FuncUtil.clickShowBigPic(Config.IMG_URL + processtask.getPhoto4(), zphoto4, ProcessdetailActivity.this);
            }
        }*/


    }


    @OnClick(R.id.confirm)
    public void onConfirmClicked() {
        RequestParams params = new RequestParams(Config.SERVER_URL + "processtasks/processtasks/updateStatus");
        params.addBodyParameter("id", String.valueOf(processtask.getId()));
        params.addBodyParameter("status", "1");
        NetUtil.postData(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                if (result.equals("success")) {
                    ToastUtil.show(ProcessdetailActivity.this, "确认成功");
                    finish();
                    app.exit();

                } else {
                    ToastUtil.show(ProcessdetailActivity.this, "确认失败");
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
            }
        });
    }

    @OnClick(R.id.reject)
    public void onRejectClicked() {
        RequestParams params = new RequestParams(Config.SERVER_URL + "processtasks/processtasks/updateStatus");
        params.addBodyParameter("id", String.valueOf(processtask.getId()));
        params.addBodyParameter("status", "2");
        NetUtil.postData(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                if (result.equals("success")) {
                    FuncUtil.updateno(processtask.getPuserid());
                    ToastUtil.show(ProcessdetailActivity.this, "驳回成功");
                    app.exit();
                    finish();

                } else {
                    ToastUtil.show(ProcessdetailActivity.this, "驳回失败");
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
            }
        });
    }


    @OnClick(R.id.terminal)
    public void onViewClicked() {
        RequestParams params = new RequestParams(Config.SERVER_URL + "processtasks/processtasks/updateStatus");
        params.addBodyParameter("id", String.valueOf(processtask.getId()));
        params.addBodyParameter("status", "3");
        NetUtil.postData(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                if (result.equals("success")) {
                    ToastUtil.show(ProcessdetailActivity.this, "结束任务成功");
                    app.exit();
                    finish();

                } else {
                    ToastUtil.show(ProcessdetailActivity.this, "结束任务失败");
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
            }
        });

    }

    @OnClick(R.id.play)
    public void onPlayClicked() {
        RecordingItem recordingItem = new RecordingItem();
        SharedPreferences sharePreferences = getSharedPreferences("sp_name_audio", MODE_PRIVATE);
        String filePath = Config.base_url + processtask.getRecordurl();
        int elpased = Integer.valueOf(processtask.getRecordlength());
        recordingItem.setFilePath(filePath);
        recordingItem.setLength(elpased);
//        recordingItem.setLength((int)6);
        PlaybackDialogFragment fragmentPlay = PlaybackDialogFragment.newInstance(recordingItem);
        fragmentPlay.show(getSupportFragmentManager(), PlaybackDialogFragment.class.getSimpleName());
    }

    @OnClick(R.id.lookmapBtn)
    public void onlookmapClicked() {
        Intent i = new Intent();
        if (!TextUtils.isEmpty(taskgrid.getLatitude())) {
            i.putExtra("latitude", taskgrid.getLatitude());
        }
        if (!TextUtils.isEmpty(taskgrid.getLongitude())) {
            i.putExtra("longitude", taskgrid.getLongitude());
        }
        i.setClass(this, LookmapActivity.class);
        startActivityForResult(i, 1);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }
}
